<template>
  <div class="container">
    <template v-if="!userInfoPopup">
      <div class="info-wrap">
        <img class="headimg" :src="user && user.avatarUrl || ''" alt="">
        <div class="mt10">{{user && user.nickName || ''}}</div>
      </div>

      <div class="footer padding12">
        <button type="primary"
                open-type="getPhoneNumber"
                @getphonenumber="getphonenumber">登录</button>
      </div>
    </template>

    <div class="user-info-popup" v-if="userInfoPopup">
      <div class="user-info">
        <div class="title mb20">提示</div>
        <div class="mb20 fs16 c666">授权获取用户信息</div>
        <button type="primary"
                open-type="getUserInfo"
                @getuserinfo="getuserinfo">授权</button>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss" rel="stylesheet/scss">
  .user-info-popup {
    position: fixed;
    height: 100%;
    width: 100%;
    background: rgba(0, 0, 0, 0.5);
    .user-info {
      position: absolute;
      top: 50%;
      left: 50%;
      width: 70%;
      transform: translate(-50%, -50%);
      padding: 20px;
      border-radius: 6px;
      text-align: center;
      background: #FFFFFF;
    }
  }
  .container {
    height: 100vh;
    background: #ffffff;
    .info-wrap {
      display: flex;
      flex-direction: column;
      align-items: center;
      padding: 40rpx 24rpx 24rpx 24rpx;
      .headimg {
        width: 200rpx;
        height: 200rpx;
        border-radius: 100%;
      }
    }
  }
</style>

<script>
  import {mapGetters, mapActions} from 'vuex'
  import goShare from '@/mixins/goShare'

  export default {
    mixins: [goShare],
    data() {
      return {
        user: null,
        phone: '',
        userInfoPopup: false,
        encryptedData:'',
        iv:'',
      }
    },
    components: {
    },
    computed: {
      ...mapGetters(['location', 'scopeId', 'storeId', 'wxID', 'userInfo', 'shareUrl', 'sharePageUrl', 'openId','getUserSharePhone','getEmployeeType']),
    },
    methods: {
      ...mapActions(['updateAppSetting']),
      getuserinfo (e) {
        console.log(e,'这是登录信息')
        console.log(e.target.encryptedData,'这是登录信息')
        if (e.target.errMsg == 'getUserInfo:ok') {
          this.userInfoPopup = false
          this.user = e.target.userInfo
          this.encryptedData = e.target.encryptedData
          this.iv = e.target.iv
        }
      },
      getphonenumber (e) {
        console.log(e,111)
        if (!e.target.encryptedData || !e.target.iv) {
          return
        }

        wx.showLoading()
        this.$api.getUserPhone({
          encryptedData: e.target.encryptedData,
          iv: e.target.iv
        }).then(data => {
          wx.hideLoading()

          this.phone = data
          this.submit()
        }, err => {
          wx.hideLoading()
          err.msg && wx.showModal({
            title: '提示',
            content: err.msg,
            showCancel: false,
            success(res) {}
          })
        })
      },
      //  登录提交
      submit() {
        //  test
        // this.updateAppSetting({
        //   userInfo: {"userId":"cd741d9582b74b49a8d2d019a77840a2","username":"WeChat_514095827308908544","phone":"13564358146","email":"","password":"WeChat_蒋蒋蒋蒋菇凉","salt":"","isActive":true,"isPwd":false,"isLock":false,"nickname":"蒋蒋蒋蒋菇凉","sex":2,"avatar":"http://thirdwx.qlogo.cn/mmopen/vi_32/xfhgk8wR3DpYoefRGt5FnNWFHj96QeDibSwDHtq0bEicEoMjFib7PiaIBKtPdC8FbFoNEYMu0iaZVc6T14lBa2rzC9w/132","createTime":1542611597000,"createIp":"210.13.80.220","lastLoginTime":1561468305000,"lastLoginIp":"112.65.48.227","lastLoginCity":39,"activeTime":"","oauthType":"","userOauthId":"","updateTime":"","inviterId":"","userNo":"M0000081072"},
        // })
        // this.goSharePage('register')
        // return

        wx.showLoading()
        this.$api.register({
          phone: this.phone,
          avatar: this.user.avatarUrl,
          openId: this.openId,
          userName: this.user.nickName,
          sex: this.user.gender,
          wxId: this.wxID,
          scopeId: this.scopeId,
          storeId: this.storeId,
          encryptedData:this.encryptedData,
          iv:this.iv,
        }).then(data => {
          wx.hideLoading()
          this.updateAppSetting({
            userInfo: data
          })

          this.goSharePage('register')
          console.log(this.getUserSharePhone,"收到分享人手机号调接口")
          if (this.getUserSharePhone) {
          let params = {
            phone :this.getUserSharePhone|| "",
            employeeType: this.getEmployeeType|| ""
          }
          this.$api.bindEmployee(params).then(data => {
          }, err => {})
        }
        }, err => {
          wx.hideLoading()
          err.msg && wx.showModal({
            title: '提示',
            content: err.msg,
            showCancel: false,
            success(res) {}
          })
        })
      },
    },
    onLoad(options) {
      this.userInfoPopup = true
    },
  }
</script>

